import json

from django.shortcuts import render
from django.http import HttpResponse


# Create your views here.


def get(request, province, phone):
    response = HttpResponse('get info')
    response.set_cookie('province', province)
    response.set_cookie('phone', phone)
    return response


def get_(request):
    info = request.GET
    print(type(info))
    name = info['name']
    print(name)
    namelist = info.getlist('name')
    print(namelist)
    return HttpResponse('get personal info')


def post(request):
    # print(request.META)
    data = request.POST
    data_body = request.body
    print(data)
    print(data_body)
    user = data.get('user')
    password = data.get('password')
    print(user, password)
    response = HttpResponse('post')
    response.set_cookie('user', user)
    response.set_cookie('password', password)
    request.session['user'] = user
    request.session['password'] = password
    return response


def post_json(request):
    data = json.loads(request.body.decode())
    print(data)
    type = request.META['CONTENT_TYPE']
    print(type)
    return HttpResponse('post_json')


from django.http import JsonResponse


def json_response(request):
    return JsonResponse({"name": "SALAY", "age": "16"})


def http_json_response(request):
    j = {"name": "SALAY", "age": "16"}
    data = json.dumps(j)
    return HttpResponse(content=data, content_type='application/json')


def axios_get(request):
    data = request.GET
    print(data)
    name = data['name']
    age = data['age']
    return JsonResponse({"name": name, "age": age})


def axios_post(request):
    data_body = request.body
    data = request.POST
    print('data:', data)
    print('data_body:', data_body)
    # 表单传参
    # name = data.get('name')
    # age = data.get('age')
    # json传参
    data_ = json.loads(data_body.decode())
    name = data_['name']
    age = data_['age']
    return JsonResponse({"name": name, "age": age})
